package com.gking.resourcePool.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.resourcePool.common.Result;
import com.gking.resourcePool.service.Resource.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    /**
     * 资源批量上传
     *
     * @param projectId 项目id
     * @param groupId   项目分组id
     * @param resources 上传的资源列表
     * @return s
     */
    @RequiresPermissions("RBS:resource:upload")
    @PostMapping("/upload")
    public Result<String> uploadResources(@RequestParam("projectId") String projectId,
                                          @RequestParam("groupId") String groupId,
                                          List<MultipartFile> resources) {
        if (projectId.isEmpty() || resources.isEmpty()) return Result.error("参数不完整");
        try {
            return resourceService.uploadResources(projectId, groupId, resources);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改资源分组
     *
     * @param resourceId 资源id
     * @param projectId  项目id
     * @param groupId    分组id
     * @return s
     */
    @RequiresPermissions("RBS:resource:modify")
    @PostMapping("/changeResourceGroup/{id}")
    public Result<String> changeResourceGroup(@PathVariable("id") String resourceId,
                                              @RequestParam String projectId, @RequestParam String groupId) {
        if (resourceId.isEmpty() || projectId.isEmpty() || groupId.isEmpty())
            return Result.error("请将参数补充完整");
        try {
            return resourceService.changeResourceGroup(resourceId, projectId, groupId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量删除资源
     *
     * @param resourceIds 资源 id 列表
     * @return s
     */
    @RequiresPermissions("RBS:resource:delete")
    @PostMapping("/deleteResources")
    public Result<String> deleteResources(@RequestBody List<String> resourceIds) {
        if (resourceIds.isEmpty()) return Result.error("参数不完整");
        try {
            return resourceService.deleteResources(resourceIds);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页获取资源列表
     *
     * @param projectId 项目id
     * @param groupId   分组id
     * @param size      分页大小
     * @param page      分页页码
     * @return l
     */
    @RequiresPermissions("RBS:resource:get")
    @GetMapping("/resourceList/{projectId}")
    public Result<Page> getResources(@PathVariable("projectId") String projectId, @RequestParam String groupId,
                                     @RequestParam Integer size, @RequestParam Integer page) {
        if (projectId.isEmpty() || size == null || page == null) return Result.error("参数不完整");
        try {
            return resourceService.getResources(projectId, groupId, size, page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取删除资源列表
     *
     * @param size 分页大小
     * @param page 分页页码
     * @return l
     */
    @RequiresPermissions("RBS:resource:get")
    @GetMapping("/getDeleteResources")
    public Result<Page> getDeleteResources(@RequestParam Integer size, @RequestParam Integer page) {
        if (size == null || page == null) return Result.error("参数不完整");
        try {
            return resourceService.getDeleteResources(size, page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 批量恢复资源
     *
     * @param resourceIds 要恢复的资源 id 列表
     * @return s
     */
    @RequiresPermissions("RBS:resource:recover")
    @PostMapping("/recoverResources")
    public Result<String> recoverResources(@RequestBody List<String> resourceIds) {
        if (resourceIds.isEmpty()) return Result.error("参数不完整");
        try {
            return resourceService.recoverResources(resourceIds);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
